package cn.edu.ujn.food.service;

import cn.edu.ujn.food.entity.Order;
import cn.edu.ujn.food.dao.OrderDao;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;

    @Override
    public Order queryById(Integer orderId  ) {
        return this.orderDao.queryById(orderId );
    }

    @Override
    public Page<Order> queryByPage(Order order, PageRequest pageRequest) {
        long total = this.orderDao.count(order);
        return new PageImpl<>(this.orderDao.queryAllByLimit(order, pageRequest), pageRequest, total);
    }

    @Override
    public Order insert(Order order) {
        this.orderDao.insert(order);
        return order;
    }

    @Override
    public boolean deleteById(Integer orderId ) {
        return this.orderDao.deleteById(orderId ) > 0;
    }

    @Override
    public cn.edu.ujn.util.Page<Order> selectAll(Order order) {
        List<Order> orders = orderDao.selectAll(order);
        cn.edu.ujn.util.Page<Order> page = new cn.edu.ujn.util.Page<>();
        page.setPage(order.getStart());
        page.setRows(orders);
        page.setSize(order.getRows());
        page.setTotal(orderDao.selectCount());
        return page;
    }

    @Override
    public Order update(Order order) {
        this.orderDao.update(order);
        return this.queryById(order.getOrderId());
    }
}
